package com.ruoyi.pingjiao.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pingjiao.domain.Student;
import com.ruoyi.pingjiao.mapper.StudentMapper;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysDeptMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pingjiao.mapper.MetaclassMapper;
import com.ruoyi.pingjiao.domain.Metaclass;
import com.ruoyi.pingjiao.service.IMetaclassService;

/**
 * 行政班级管理Service业务层处理
 *
 * @author 胡浩鹏
 * @date 2022-10-15
 */
@Service
@Slf4j
public class MetaclassServiceImpl implements IMetaclassService
{
    @Autowired
    private MetaclassMapper metaclassMapper;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private StudentMapper studentMapper;
    /**
     * 查询行政班级管理
     *
     * @param meataclassId 行政班级管理主键
     * @return 行政班级管理
     */
    @Override
    public Metaclass selectMetaclassByMeataclassId(Long meataclassId)
    {
        return metaclassMapper.selectMetaclassByMeataclassId(meataclassId);
    }

    /**
     * 查询行政班级管理列表
     *
     * @param metaclass 行政班级管理
     * @return 行政班级管理
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<Metaclass> selectMetaclassList(Metaclass metaclass)
    {
        return metaclassMapper.selectMetaclassList(metaclass);
    }

    /**
     * 新增行政班级管理
     *
     * @param metaclass 行政班级管理
     * @return 结果
     */
    @Override
    public int insertMetaclass(Metaclass metaclass)
    {
        metaclass.setCreateTime(DateUtils.getNowDate());
        return metaclassMapper.insertMetaclass(metaclass);
    }

    /**
     * 修改行政班级管理
     *
     * @param metaclass 行政班级管理
     * @return 结果
     */
    @Override
    public int updateMetaclass(Metaclass metaclass)
    {
        metaclass.setUpdateTime(DateUtils.getNowDate());
        return metaclassMapper.updateMetaclass(metaclass);
    }

    /**
     * 批量删除行政班级管理
     *
     * @param meataclassIds 需要删除的行政班级管理主键
     * @return 结果
     */
    @Override
    public int deleteMetaclassByMeataclassIds(Long[] meataclassIds)
    {
        return metaclassMapper.deleteMetaclassByMeataclassIds(meataclassIds);
    }

    /**
     * 删除行政班级管理信息
     *
     * @param meataclassId 行政班级管理主键
     * @return 结果
     */
    @Override
    public int deleteMetaclassByMeataclassId(Long meataclassId)
    {
        return metaclassMapper.deleteMetaclassByMeataclassId(meataclassId);
    }

    @Override
    public String importStudent(List<Metaclass> metaclasses, String operName) {
        if (StringUtils.isNull(metaclasses) || metaclasses.isEmpty()) {
            throw new ServiceException("导入学生数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (Metaclass metaclass : metaclasses) {

            try {
                Metaclass metaclass1 = metaclassMapper.selectMetaclassByMetaclassName(metaclass.getMetaclassName());
                SysDept sysDept = deptMapper.selectDeptByDeptName(metaclass.getDeptName());
//                // 根据学号查询学生
//                Student stuResult2 = studentMapper.selectStudentByStudentNo(student.getStudentNo());


                if ( StringUtils.isNull(metaclass1)&&!StringUtils.isNull(sysDept) ) {
                    metaclass.setDeptId(sysDept.getDeptId());
                    metaclassMapper.insertMetaclass(metaclass);
//                    student.setProfessionId(metaclass.getProfessionId());
//                    // 不存在
//                    // 插入 学生
//                    SysUser sysUser = randomStudentUser(student);
//                    student.setCreateBy(SecurityUtils.getUsername());
//                    student.setUserId(sysUser.getUserId());
//                    student.setCreateTime(DateUtils.getNowDate());
//                    studentMapper.insertStudent(student);
//                    SysUserRole sysUserRole=new SysUserRole();
//                    sysUserRole.setRoleId(Long.valueOf(107));
//                    sysUserRole.setUserId(sysUser.getUserId());
//                    sysUserRoleMapper.insert(sysUserRole);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、班级 " + metaclass.getMetaclassName() + " 导入成功");
                } else if (!StringUtils.isNull(metaclass1) && !StringUtils.isNull(metaclass) ) {
                    // 存在  不支持更新
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、班级 " + metaclass.getMetaclassName() + "学号已存在");
                } else {
                    // 存在  不支持更新
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、班级 " + metaclass.getMetaclassName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、班级 " + metaclass.getMetaclassName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }

        }
        if (failureNum > 0) {
            failureMsg.insert(0, "共 " + failureNum + " 条数据格式不正确并导入失败，错误如下：");
        }
        if (successNum > 0) {
            successMsg.insert(0, "共 " + successNum + " 条导入成功，数据如下：");
        }
        return successMsg.toString() + "<br/>" + failureMsg.toString();
    }

    @Override
    public List<Student> selectStudentByMeataclassId(Long meataclassId) {
        List<Student> students = studentMapper.selectStudentListByMeataclassId(meataclassId);
        return students;
    }
}
